package finalyearproject;

import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;

public class FinalYearProject {

    public static void main(String[] args) 
    {
        testSite();        
    }
    
    static void testSite()
    {
        try
        {
            //File webTestingTest = new File("c:\\test.html");//
            URL webTest = new URL("http://localhost/test.html");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(webTest.openStream());           
            doc.getDocumentElement().normalize();
            
            System.out.println("Testing site @ " + webTest.toString());
            //System.out.println("Root Element : " + doc.getDocumentElement().getNodeName());
            NodeList nodeList = doc.getElementsByTagName("*"); // Gets ALL tags
            System.out.println("----------------------");
            
            Map htmlTagMap = new HashMap();// Hashmap of HTML tags and how many times they appear
            
            for(int i = 0; i < nodeList.getLength(); i++) // for each tag in the whole page
            {
                Element element = (Element)nodeList.item(i);
                String htmlTag = element.getNodeName();
                
                if(element.hasAttributes()) // If the tag has got attributes href id etc..
                { 
                    NamedNodeMap attributes = element.getAttributes(); // List of all attributes

                    for(int x=0;x < attributes.getLength(); x++)
                    {
                        String tagAttributeName = attributes.item(x).getNodeName();
                        String tagAttributeValue = attributes.item(x).getNodeValue();
                        System.out.println("HTML Tag : <" + htmlTag + "> has an attribute of : " + 
                                tagAttributeName + " with a value of : " + tagAttributeValue);
                    }
                }
                
                if(htmlTagMap.containsKey(htmlTag)) // if there is already a value with that html tag in the map
                {
                    String value = (String) htmlTagMap.get(htmlTag);
                    int parseValue = Integer.parseInt(value);
                    parseValue++;
                    htmlTagMap.put(htmlTag, parseValue++); 
                }
                else
                {
                htmlTagMap.put(htmlTag, "1"); // Adds the current HTML Tag into the hash map
                }
                //System.out.println(element.getNodeName()); // Prints out the type of html tag it is
                //System.out.println(element.getTextContent()); // Gets content between the tag and tag ending
                
            }
            
            System.out.println("--------------");
            
            System.out.println(htmlTagMap);
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
